#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Set_up.py
@Time    :   2022/04/13 18:00:00
@Author  :   WangWei 
@Version :   1.0
@Contact :   wangwei@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''
# here put the import lib

import datetime
import  os
from ssl import CHANNEL_BINDING_TYPES
from xml.dom.expatbuilder import FilterVisibilityController


# HW config
#CAN
drive_type = 'USE_TS_TC1014_DEVICE'   # USE_TS_TC1014_DEVICE /   USE_TS_VIRTUAL_DEVICE / USE_TS_TC1011_DEVICE

CAN_Type = 'CANFD'
if CAN_Type == 'CAN':
    configure_baudrate = {'CAN_Type':'CAN','ArbRate':500,'DataRate':500,'TermResistor':True}
else:
    configure_baudrate = {'CAN_Type':'CANFD','ArbRate':500,'DataRate':2000,'TermResistor':True}

CAN_Type2 = 'CANFD'
if CAN_Type2 == 'CAN':
    configure_baudrate2 = {'CAN_Type':'CAN','ArbRate':500,'DataRate':500,'TermResistor':True}
else:
    configure_baudrate2 = {'CAN_Type':'CANFD','ArbRate':500,'DataRate':2000,'TermResistor':True}


#Dbc define
root = os.getcwd()
project_DBC_File_CAN = os.path.join(root, r'data\MRR_20230404.dbc')
blfname = datetime.datetime.now().strftime('%y%m%d') + '.blf'
project_BLF_File_CAN = os.path.join(root, f"blf\{blfname}")
# project_DBC_File_CAN = os.path.join(root, r'data\FX12_Matrix_CRRL_V1.6_20220531.dbc')
NetName = 'Vehicle_CAN'
nodeName= 'BCM'
# EnableNode = ['CGW_Safety','EPS_Safety','APA_Safety','ABS_ESP_Safety','FVC_Safety']
EnableNode = ['MRR','BCM']
EnableNode2 = ['EDR']

#Power Config
source='Power'           #电源名称，可以使用NIMAX查看
baudrate=9600

#Dbc UDS
reqID = 0x7F2
resID = 0x7FA
AFctID = 0x7DF

modbusCOM = 'COM8'

# 0x22 DID
# 底层软件Bootload版本号 F180
BootSwId= [0x56,0x31,0x2E,0x32,0x30,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20] # 0xF180
# 备用零件号
CowinSparePartNumber = [0x46,0x30,0x32,0x2D,0x33,0x36,0x30,0x32,0x30,0x31,0x34,0x2D,0x30,0x31,0x41,0x41,0x20,0x20,0x20,0x20]  # 0xF187
# 软件版本号
ManufacturerECUSoftwareVersionNumber = [0x30,0x30,0x2E,0x30,0x30,0x2E,0x30,0x33] # 0xF188
# 生成日期
ProgrammingDate = [0x20,0x22,0x07,0x25]  # 0xF199
# 雷达角度
Radarangle = 0x2D # 0x3F1A

softname = 'MRR_HA3H_QinPlus_J3_App_3.01.02_Boot_1.1_CT_6.1.12.3_6.1.12.2_Pro.XCDII.zip'

zipfile_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +softname
error_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'FailedSwPlus.zip'
oldzipfile_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'old.zip'
errorApp_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'FailedSwPlus\\001_Failed_App.hex'
errorBoot_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'FailedSwPlus\\002_Failed_Boot.hex'

CTBoot=[6,1,12,1]

'''BYD产品DID'''
# 硬件版本号 F193 
ECUHardwareVersionNumber = '0b 17 0a 08 00'# 美信【01 16 09 06 00】      70331【0b 17 09 0d 00】  4323【05 16 0c 1f 00 】 9840【06 16 0c 1f 00 】 '0b 17 0a 08 00':'MPC_70331_230Pro',
# 软件编码 F194
ECUSoftwareCode = '4A 55 00 12 09 70 00 00 00'
# DATASET软件序列号 FF94
DATASETSoftwareNumber = []
# DATASET软件版本号 FF95
DATASETSoftwareVersionNumber = []
# 底层软件Bootload版本号 F180
bootyear = [23]
bootmonth = [12]
bootday = [28]
bootchangeNum = [0]
bootver = [11]
bootweek = [52]
# bootweek.append(datetime.date(2000 + bootyear[0], bootmonth[0], bootday[0]).isocalendar().week)
VehicleManufacturerECUBootSoftwareVersionNumber = [0x62,0xF1,0X80] + bootyear + bootweek + bootchangeNum + bootver + [0] + [0] + bootyear + bootmonth + bootday

# 软件版本号 F195
APP_ver = '30102'
softver = []
hexver = hex(int(APP_ver)).replace('0x','').zfill(4)
softver.append(int(hexver[0:2],16))
softver.append(int(hexver[2:4],16))
softyear =[23]
softmonth = [12]
softday = [28]
ECUSoftwareVersionNumber = softver + softyear + softmonth + softday + [0]


# 生产模式 3401
FactoryMode = []
# 配置字
VariantCoding = [   ('12 0E 70 01 00 00 00 C4'),              
                    # ('10 CF 70 02 00 00 00 42'),
                    ('12 09 70 03 00 00 00 DB'),  
                ]

# 初始存在故障
InitialFailure = [[0xAC,0xD1,0x78,0x09],[0xAC,0xD2,0x78,0x09],[0xAC,0xE6,0x55,0x09]]

Dut_Type = 'HA3H_QinPlus_J3'




DTC_list = [    ([0xAC,0xCE,0x17],'DTC_VBAT_HIGH'),			
                ([0xAC,0xCF,0x16],'DTC_VBAT_LOW'),		
				([0xC0,0x73,0x88],'DTC_PCAN_BUSOFF'),			
				([0xC1,0xD7,0x88],'DTC_SFCAN_BUSOFF'),			
				([0xAC,0xD0,0x00],'DTC_PRODUCTION_MODE_ACTIVE'),
                ([0xAC,0xD1,0x78],'DTC_ALIGNMENT_NEVER_DONE'),
                ([0xAC,0xD2,0x78],'DTC_ALIGNMENT_NOT_DONE'),
                ([0xAC,0xE6,0x55],'DTC_VARIANT_NOT_CODED'),
                ([0xAC,0xE8,0x76],'DTC_MRR_SYSTEM_BLIND'),
                ([0xAC,0xE9,0x76],'DTC_MRR_SENSOR_BLIND'),
                ([0xAC,0xEA,0x94],'DTC_MRR_VERTICAL_MISALIGNED'),
                ([0xAC,0xEB,0x94],'DTC_MRR_HORIZONTAL_MISALIGNED'),
                ([0xAC,0xD4,0x88],'DTC_FACTORYDATA_FAILURE'),
                ([0xAC,0xD6,0x40],'DTC_INTERNAL_POWER_MANAGEMENT_SYS_FAILURE'),
                ([0xAC,0xD8,0x04],'DTC_INTERNAL_HW_FAILURE'),
                ([0xAC,0xD9,0x17],'DTC_POWER_MANAGEMENT_CHIP_VOLTAGE_HIGH'),
                ([0xAC,0xDB,0x16],'DTC_POWER_MANAGEMENT_CHIP_VOLTAGE_LOW'),
                ([0xAC,0xDD,0x00],'DTC_RADAR_MODULATION_CONFIGURE'),
                ([0xAC,0xDE,0x00],'DTC_RADAR_MODULATION'),
                ([0xAC,0xDF,0x52],'DTC_SW_FAILURE'),
                ([0xAC,0xE0,0x00],'DTC_SW_RIF_FAILURE'),
                ([0xAC,0xE1,0x52],'DTC_SW_TEMP_FAILURE'),
                ([0xAC,0xE2,0x4B],'DTC_UC_TEMPERATURE_OUTOFRANGE'),
                ([0xAC,0xED,0x00],'DTC_AntennaDiagram_INVALID'),
                ([0xC1,0xA8,0x87],'DTC_Left_BCM_TIMEOUT'),
                ([0x5C,0x67,0x62],'DTC_Left_BCM_CRC'),
                ([0x5C,0x66,0x02],'DTC_Left_BCM_ALC'),
                ([0x5C,0x65,0x86],'DTC_Left_BCM_Signal_Invalid'),
                ([0xC2,0x45,0x00],'DTC_Media_TIMEOUT'),
                ([0xD0,0xC2,0x86],'DTC_VCU_TIMEOUT'),
                ([0xD2,0x71,0x83],'DTC_VCU_CRC'),
                ([0xD2,0x71,0x82],'DTC_VCU_ALC'),
                ([0xC1,0x97,0x80],'DTC_IPB_TIMEOUT'),
                ([0x5C,0x64,0x62],'DTC_IPB_CRC'),
                ([0x5C,0x63,0x02],'DTC_IPB_ALC'),
                ([0x5C,0x62,0x86],'DTC_IPB_Signal_Invalid'),
                ([0xC1,0x34,0x00],'DTC_EPS_TIMEOUT'),
                ([0xC4,0x20,0x83],'DTC_EPS_CRC'),
                ([0xC4,0x20,0x82],'DTC_EPS_ALC'),
                ([0xD0,0x17,0x86],'DTC_EPS_Signal_Invalid'),
                ([0xD1,0x18,0x86],'DTC_MPC_TIMEOUT')
            ]

DTC_list2 = {   'DTC_VBAT_HIGH':                                [0xAC,0xCE,0x17],		
                'DTC_VBAT_LOW':                                 [0xAC,0xCF,0x16],		
				'DTC_PCAN_BUSOFF':                              [0xC0,0x73,0x88],			
				'DTC_SFCAN_BUSOFF':                             [0xC1,0xD7,0x88],			
				'DTC_PRODUCTION_MODE_ACTIVE':                   [0xAC,0xD0,0x00],
                'DTC_ALIGNMENT_NEVER_DONE':                     [0xAC,0xD1,0x78],
                'DTC_ALIGNMENT_NOT_DONE':                       [0xAC,0xD2,0x78],
                'DTC_VARIANT_NOT_CODED':                        [0xAC,0xE6,0x55],
                'DTC_MRR_SYSTEM_BLIND':                         [0xAC,0xE8,0x76],
                'DTC_MRR_SENSOR_BLIND':                         [0xAC,0xE9,0x76],
                'DTC_MRR_VERTICAL_MISALIGNED':                  [0xAC,0xEA,0x94],
                'DTC_MRR_HORIZONTAL_MISALIGNED':                [0xAC,0xEB,0x94],
                'DTC_FACTORYDATA_FAILURE':                      [0xAC,0xD4,0x88],
                'DTC_INTERNAL_POWER_MANAGEMENT_SYS_FAILURE':    [0xAC,0xD6,0x40],
                'DTC_INTERNAL_HW_FAILURE':                      [0xAC,0xD8,0x04],
                'DTC_POWER_MANAGEMENT_CHIP_VOLTAGE_HIGH':       [0xAC,0xD9,0x17],
                'DTC_POWER_MANAGEMENT_CHIP_VOLTAGE_LOW':        [0xAC,0xDB,0x16],
                'DTC_RADAR_MODULATION_CONFIGURE':               [0xAC,0xDD,0x00],
                'DTC_RADAR_MODULATION':                         [0xAC,0xDE,0x00],
                'DTC_SW_FAILURE':                               [0xAC,0xDF,0x52],
                'DTC_SW_RIF_FAILURE':                           [0xAC,0xE0,0x00],
                'DTC_SW_TEMP_FAILURE':                          [0xAC,0xE1,0x52],
                'DTC_UC_TEMPERATURE_OUTOFRANGE':                [0xAC,0xE2,0x4B],
                'DTC_AntennaDiagram_INVALID':                   [0xAC,0xED,0x00],
                'DTC_Left_BCM_TIMEOUT':                         [0xC1,0xA8,0x87],
                'DTC_Left_BCM_CRC':                             [0x5C,0x67,0x62],
                'DTC_Left_BCM_ALC':                             [0x5C,0x66,0x02],
                'DTC_Left_BCM_Signal_Invalid':                  [0x5C,0x65,0x86],
                'DTC_Media_TIMEOUT':                            [0xC2,0x45,0x00],
                'DTC_VCU_TIMEOUT':                              [0xD0,0xC2,0x86],
                'DTC_VCU_CRC':                                  [0xD2,0x71,0x83],
                'DTC_VCU_ALC':                                  [0xD2,0x71,0x82],
                'DTC_IPB_TIMEOUT':                              [0xC1,0x97,0x80],
                'DTC_IPB_CRC':                                  [0x5C,0x64,0x62],
                'DTC_IPB_ALC':                                  [0x5C,0x63,0x02],
                'DTC_IPB_Signal_Invalid':                       [0x5C,0x62,0x86],
                'DTC_EPS_TIMEOUT':                              [0xC1,0x34,0x00],
                'DTC_EPS_CRC':                                  [0xC4,0x20,0x83],
                'DTC_EPS_ALC':                                  [0xC4,0x20,0x82],
                'DTC_EPS_Signal_Invalid':                       [0xD0,0x17,0x86],
                'DTC_MPC_TIMEOUT':                              [0xD1,0x18,0x86]
}

PCAN_list = {   0x80: [0,True,0],    # rollcounter值  是否第一次=True:one, 出错次数
                0x20: [0,True,0],
                0x21: [0,True,0],
                0x22: [0,True,0],
                0x23: [0,True,0],
                0x24: [0,True,0],
                0x25: [0,True,0],
                0x26: [0,True,0],
                0x27: [0,True,0],
                0x28: [0,True,0],
                0x29: [0,True,0],
                0x2A: [0,True,0],
                0x2B: [0,True,0],
                0x2C: [0,True,0],
                0x2D: [0,True,0],
                0x2E: [0,True,0],
                0x2F: [0,True,0],
                0x30: [0,True,0],
                0x31: [0,True,0],
                0x32: [0,True,0],
                0x33: [0,True,0],
                0x34: [0,True,0],
                0x35: [0,True,0],
                0x36: [0,True,0],
                0x37: [0,True,0],
                0x38: [0,True,0],
                0x39: [0,True,0],
                0x3A: [0,True,0],
                0x3B: [0,True,0],
                0x3C: [0,True,0],
                0x3D: [0,True,0],
                0x3E: [0,True,0],
                0x3F: [0,True,0],
                0x40: [0,True,0],
                0x41: [0,True,0],
                0x42: [0,True,0],
                0x43: [0,True,0],
                0x44: [0,True,0],
                0x45: [0,True,0],
                0x46: [0,True,0],
                0x47: [0,True,0],
                0x50: [0,True,0],
                0x51: [0,True,0],
                0x52: [0,True,0],
                0x53: [0,True,0],
                0x54: [0,True,0],
                0x55: [0,True,0],
                0x56: [0,True,0],
                0x57: [0,True,0],
                0x58: [0,True,0],
                0x59: [0,True,0],
                0x5A: [0,True,0],
                0x5B: [0,True,0],
                0x5C: [0,True,0],
                0x5D: [0,True,0],
                0x5E: [0,True,0],
                0x5F: [0,True,0],
                0x60: [0,True,0],
                0x61: [0,True,0],
                0x62: [0,True,0],
                0x63: [0,True,0],
                0x64: [0,True,0],
                0x65: [0,True,0],
                0x66: [0,True,0],
                0x67: [0,True,0],
                0x68: [0,True,0],
                0x69: [0,True,0],
                0x6A: [0,True,0],
                0x6B: [0,True,0],
                0x6C: [0,True,0],
                0x6D: [0,True,0],
                0x6E: [0,True,0],
                0x6F: [0,True,0],
                0x70: [0,True,0],
                0x71: [0,True,0],
                0x72: [0,True,0],
                0x73: [0,True,0],
                0x74: [0,True,0],
                0x75: [0,True,0],
                0x76: [0,True,0],
                0x77: [0,True,0]                
}

PCAN_list_BOCH = {  0x64E: [0,True,0,8],
                    0x66F: [0,True,0,16],
                    0x670: [0,True,0,64],
                    0x671: [0,True,0,64],
                    0x672: [0,True,0,64],
                    0x673: [0,True,0,64],
                    0x674: [0,True,0,64],
                    0x675: [0,True,0,64],
                    0x676: [0,True,0,64],
                    0x677: [0,True,0,64],
                    0x678: [0,True,0,64],
                    0x679: [0,True,0,64],
                    0x67A: [0,True,0,64],
                    0x67B: [0,True,0,64],
                    0x67C: [0,True,0,64],
                    0x67D: [0,True,0,64],
                    0x67E: [0,True,0,64],
                    0x67F: [0,True,0,64]
}

startCheck = False

MRR_0x66F_data = []
MRR_0x66F_time = []

MRR_0x5F0_data = []



# import csv                 
# # data file
# root = os.getcwd()
# DTC_data_file = os.path.join(root, r'data\DTCdata.csv')
# # DTC_data_table = []
# # with open(DTC_data_file, 'r') as f:
# #     for row in csv.reader(f):
# #         DTC_data_table.append(row)
# # DTC_data = DTC_data_table[1:]

# import pandas as pd
# import numpy as np
# data = pd.read_csv(DTC_data_file)
# print(data)
# act_vehicle_type = 'A02'
# DTC_data = np.array(data).tolist()
# print(DTC_data)
# if act_vehicle_type in DTC_data[1][9:]:
#     print("111111")

# allure generate ..\allure-results\ -o .\report --clean
# allure open .\report



sessionControl_default = [([0x10,0x01],[0x50,0x01,0x00,0x32,0x13,0x88]),    
                          ([0x10,0x03],[0x50,0x03,0x00,0x32,0x13,0x88])]

sessionControl_extend = [([0x10,0x03],[0x50,0x03,0x00,0x32,0x13,0x88]),    
                         ([0x10,0x01],[0x50,0x01,0x00,0x32,0x13,0x88])]

sessionControl_Suppress_default = [([0x10,0x81],[]),    
                                   ([0x10,0x83],[])]    

sessionControl_Suppress_extend = [([0x10,0x83],[]),    
                                 ([0x10,0x81],[])] 

sessionControl_NRC_phy = [([0x10],[0x7F,0x10,0x13]),        
                          ([0x10,0x01,0xFF],[0x7F,0x10,0x13]),
                          ([0x10,0x03,0x10],[0x7F,0x10,0x13]),
                          ([0x10,0x05,0x06],[0x7F,0x10,0x13]),
                          ([0x10,0x04],[0x7F,0x10,0x12]),   
                          ([0x10,0x05],[0x7F,0x10,0x12]), 
                          ([0x10,0x06],[0x7F,0x10,0x12]),
                          ([0x23],[0x7F,0x23,0x11]),
                          ([0x2A],[0x7F,0x2A,0x11]),
                          ([0x2C],[0x7F,0x2C,0x11]),
                          ([0x2F],[0x7F,0x2F,0x11]),
                          ]

sessionControl_SuppressPosMsg_NRC_phy = [     
                                         ([0x10,0x81,0xFF],[0x7F,0x10,0x13]),
                                         ([0x10,0x83,0x10],[0x7F,0x10,0x13]),
                                         ([0x10,0x84],[0x7F,0x10,0x12]),   
                                         ([0x10,0x85],[0x7F,0x10,0x12]), 
                                         ([0x10,0x86],[0x7F,0x10,0x12])]

sessionControl_NRC7E_phy = [([0x10,0x02],[0x7F,0x10,0x7E])]

sessionControl_NRC13_func = [([0x10],[0x7F,0x10,0x13]),        
                             ([0x10,0x01,0xFF],[0x7F,0x10,0x13]),
                             ([0x10,0x03,0x10],[0x7F,0x10,0x13])]

sessionControl_noResp_default_func = [([0x10,0x04],[]),   
                                      ([0x10,0x05],[]), 
                                      ([0x10,0x06],[]),
                                      ([0x10,0x02],[]),
                                      ([0x27,0x03],[]),
                                      ([0x2E,0x3F,0x01],[]),
                                      ([0x28,0x00,0x01],[]),
                                      ([0x85,0x01],[]),
                                      ([0x23],[]),
                                      ([0x2A],[]),
                                      ([0x2C],[])]


sessionControl_suppressPosMsgBit_NRC13 = [([0x10,0x81,0xFF],[0x7F,0x10,0x13]),
                                          ([0x10,0x83,0x10],[0x7F,0x10,0x13])]

sessionControl_noResp_suppressPosMsgBit_default_func = [([0x10,0x84],[]),   
                                                        ([0x10,0x85],[]), 
                                                        ([0x10,0x86],[]),
                                                        ([0x10,0x82],[])]

sessionControl_noResp_suppressPosMsgBit_extend_func = [([0x10,0x84],[]),   
                                                       ([0x10,0x85],[]), 
                                                       ([0x10,0x86],[])]

boot_sessionControl_NRC = [([0x10],[0x7F,0x10,0x13]),         
                           ([0x10,0x02,0xFF],[0x7F,0x10,0x13]),
                           ([0x10,0x02,0x10],[0x7F,0x10,0x13]),
                           ([0x10,0x04],[0x7F,0x10,0x12]),      
                           ([0x10,0x05],[0x7F,0x10,0x12]),
                           ([0x10,0x06],[0x7F,0x10,0x12])]

boot_sessionControl_suppressPosMsgBit_NRC = [([0x10,0x82,0xFF],[0x7F,0x10,0x13]), 
                                             ([0x10,0x82,0x10],[0x7F,0x10,0x13]),
                                             ([0x10,0x84],[0x7F,0x10,0x12]),     
                                             ([0x10,0x85],[0x7F,0x10,0x12]),
                                             ([0x10,0x86],[0x7F,0x10,0x12])]

ClearDtcInfo_NRC13_Func = [([0x14],[0x7F,0x14,0x13]),                  
                           ([0x14,0xFF],[0x7F,0x14,0x13]),
                           ([0x14,0xFF,0xFF],[0x7F,0x14,0x13]),
                           ([0x14,0xFF,0xFF,0xFF,0xFF],[0x7F,0x14,0x13]),
                           ([0x14,0x11,0x22,0xFF,0xFF],[0x7F,0x14,0x13])]
